package ro.fortech.peaa.view;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;

import org.springframework.web.servlet.view.xslt.AbstractXsltView;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

import ro.fortech.peaa.domain.Department;

/**
 * @author andrei
 *
 */

@SuppressWarnings("deprecation")
public class DepartmentListView extends AbstractXsltView {
	
	@SuppressWarnings("unchecked")
	public Source createXsltSource(Map model, String rootName, HttpServletRequest
	        request, HttpServletResponse response) {
    
		Document document = null;
		try {
			document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
			return null;
		}
	    Element root = document.createElement(rootName);
	    List<Department> departments = (List<Department>) model.get("departments");
	    Map<Integer, List<Double>> depMap = (Map) model.get("depMap");
	    for (Department department : departments) {
	    	Element depNode = document.createElement("department");
	    	
	        // department id
	        Element idNode = document.createElement("id");
	        Text textNode = document.createTextNode(department.getId().toString());
	        idNode.appendChild(textNode);
	        depNode.appendChild(idNode);
	    	
	        // department name
	        Element nameNode = document.createElement("name");
	        textNode = document.createTextNode(department.getName());
	        nameNode.appendChild(textNode);
	        depNode.appendChild(nameNode);
	    	
	        // department manager
	        Element manNode = document.createElement("manager");
	        textNode = document.createTextNode(department.getManagerId().toString());
	        manNode.appendChild(textNode);
	        depNode.appendChild(manNode);
	        
	        // department location
	        Element locNode = document.createElement("location");
	        textNode = document.createTextNode(department.getLocationId().toString());
	        locNode.appendChild(textNode);
	        depNode.appendChild(locNode);
	        
	        Integer depId = department.getId();
	        List<Double> depSalaries = depMap.get(depId);
	    	
	        int numberOfEmp = 0;
	        if (depSalaries != null) {
	        	numberOfEmp = depSalaries.size();
	        }
	        // department number of employees
	        Element empNode = document.createElement("employees");
	        textNode = document.createTextNode(String.valueOf(numberOfEmp));
	        empNode.appendChild(textNode);
	        depNode.appendChild(empNode);
	        
	        Double avg = new Double(0);
	        if (numberOfEmp != 0) {
		        Double sum = new Double(0);
		        for (Double salary : depSalaries) {
		        	sum += salary;
		        }
		        avg = sum / depSalaries.size();
	        }
	    	
	        // department number of employees
	        Element avgNode = document.createElement("average");
	        textNode = document.createTextNode(avg.toString());
	        avgNode.appendChild(textNode);
	        depNode.appendChild(avgNode);
	    	root.appendChild(depNode);
	    }
	    return new DOMSource(root);
	}
}
